package com.bocloud.cmp.server.controller.app.resource;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

//import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bocloud.cmp.entity.app.resource.Software;
import com.bocloud.cmp.interfaces.app.resource.SoftwareService;
import com.bocloud.cmp.server.interceptor.Log;
import com.bocloud.common.model.BsmResult;
import com.bocloud.common.model.Param;
import com.bocloud.common.model.RequestUser;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.JSONTools;
import com.bocloud.common.utils.ResultTools;

/**
 * 
 * @author zjm
 * @date 2017年3月17日
 */
@RestController
@RequestMapping("/software")
public class SoftwareController {
	
	@Autowired
	private SoftwareService softwareService;

	@RequestMapping(value = "/create", method = { RequestMethod.POST })
	@Log(name = "创建镜像")
	public BsmResult create(@RequestParam(value = Common.PARAMS, required = true) String params,
			@Value(Common.REQ_USER) RequestUser user) {
		JSONObject object = jsonToObject(params);
		if (null != object) {
			Software software = objectToSoftware(object);
			return softwareService.create(user, software);
		}
		return ResultTools.formatErrResult();
	}

	@RequestMapping(value = "/list", method = { RequestMethod.POST })
	@Log(name = "镜像列表查询")
	public BsmResult list(@RequestParam(value = Common.PAGE, required = false) Integer page,
			@RequestParam(value = Common.ROWS, required = false) Integer rows,
			@RequestParam(value = Common.PARAMS, required = false) String params,
			@RequestParam(value = Common.SORTER, required = false) String sorter,
			@RequestParam(value = Common.SIMPLE, required = false) boolean simple,
			@Value(Common.REQ_USER) RequestUser user) {
		List<Param> paramList = JSONObject.parseArray(params, Param.class);
		@SuppressWarnings("unchecked")
		Map<String, String> sorterMap = JSONObject.parseObject(sorter, HashMap.class);
		return softwareService.list(page, rows, paramList, sorterMap, simple, user);
	}

	@RequestMapping(value = "/{id}/modify", method = { RequestMethod.POST })
	@Log(name = "镜像信息修改")
	public BsmResult modify(@RequestParam(value = Common.PARAMS, required = true) String params,
			@Value(Common.REQ_USER) RequestUser user) {
		JSONObject object = jsonToObject(params);
		if (null != object) {
			Software software = objectToSoftware(object);
			return softwareService.modify(software, user.getId());
		}
		return ResultTools.formatErrResult();
	}

	@RequestMapping(value = "/{id}/detail", method = { RequestMethod.GET })
	@Log(name = "镜像表详情")
	public BsmResult detail(@PathVariable(value = Common.ID) Long id, @Value(Common.REQ_USER) RequestUser user) {
		return softwareService.detail(id, user);
	}

	@RequestMapping(value = "/remove", method = { RequestMethod.POST })
	@Log(name = "删除镜像")
	public BsmResult remove(@RequestParam(value = Common.PARAMS, required = true) String params,
			@Value(Common.REQ_USER) RequestUser user) {
		JSONObject jsonObject = jsonToObject(params);
		List<Long> ids = JSON.parseArray(jsonObject.get("ids").toString(), Long.class);
		return softwareService.remove(ids, user.getId());
	}

	@RequestMapping(value = "/importImage", method = { RequestMethod.POST })
	@Log(name = "镜像导入")
	public BsmResult importImage(@RequestParam(value = Common.PARAMS, required = true) String params,
			@RequestParam(value = "registryId", required = true) Long registryId,
			@RequestParam(value = "filePath", required = true) String filePath,
			@Value(Common.REQ_USER) RequestUser user) {
		return softwareService.imports(user, registryId, filePath);
	}

	@RequestMapping(value = "/inspectImage", method = { RequestMethod.POST })
	@Log(name = "服务端镜像详情信息")
	public BsmResult inspectImage(@RequestParam(value = Common.PARAMS, required = true) String params,
			@RequestParam(value = "imageId", required = true) Long imageId, @Value(Common.REQ_USER) RequestUser user) {
		return softwareService.inspect(imageId, user);
	}

	@RequestMapping(value = "/countImage", method = { RequestMethod.POST })
	@Log(name = "镜像数据统计")
	public BsmResult countImage(@RequestParam(value = Common.PARAMS, required = true) String params,
			@Value(Common.REQ_USER) RequestUser user) {
		return softwareService.count(user);
	}

	@RequestMapping(value = "/deployImage", method = { RequestMethod.POST })
	@Log(name = "镜像部署")
	public BsmResult deployImage(@RequestParam(value = Common.PARAMS, required = true) String params,
			@Value(Common.REQ_USER) RequestUser user) {
		JSONObject object = jsonToObject(params);
		@SuppressWarnings("unchecked")
		Map<String, String> labels = JSON.parseObject(object.getString("labels"), HashMap.class);
		@SuppressWarnings("unchecked")
		Map<String, String> envs = JSON.parseObject(object.getString("envs"), HashMap.class);
		return softwareService.deploy(object.getLong("appId"), object.getLong("imageId"), object.getLong("clusterId"),
				labels, envs, user);
	}

	@RequestMapping(value = "/authorize", method = { RequestMethod.POST })
	@Log(name = "镜像授权")
	public BsmResult authorize(@RequestParam(value = Common.PARAMS, required = true) String params,
			@Value(Common.REQ_USER) RequestUser user) {
		JSONObject object = jsonToObject(params);
		return softwareService.authorize(object.getLong("imageId"), user.getId(), object.getLong("deptId"));
	}

	/**
	 * json字符串转换为json对象
	 * 
	 * @param json
	 * @return
	 */
	private final JSONObject jsonToObject(String json) {
		return JSONTools.isJSONObj(json);
	}

	/**
	 * json对象转换为软件对象
	 * 
	 * @param obj
	 * @return
	 */
	private final Software objectToSoftware(JSONObject obj) {
		return JSONObject.parseObject(obj.toJSONString(), Software.class);
	}

}
